//
//  MessageView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/23.
//

#import "MessageView.h"
#import "CommonUtil.h"
#import "UUMarqueeView.h"

@interface MessageView()<UUMarqueeViewDelegate>

@property (strong, nonatomic) UILabel * adTitleLabel;
@property (strong, nonatomic) UILabel * noticeTitleLabel;
@property (strong, nonatomic) UIView * lineView;
@property (strong, nonatomic) NSArray * adTitles;
@property (strong, nonatomic) NSArray * noticeTitles;

@property (strong, nonatomic) UUMarqueeView * adMarqueValue;
@property (strong, nonatomic) UUMarqueeView * noticeMarqueValue;
@end

@implementation MessageView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = AD(10);
        self.clipsToBounds = YES;
        [self addSubview:self.adTitleLabel];
        [self addSubview:self.adMarqueValue];
        [self addSubview:self.noticeTitleLabel];
        [self addSubview:self.noticeMarqueValue];
        [self addSubview:self.lineView];
        
        self.adTitles= @[@"Do not go gentle into that good night,",
                                             @"Old age should burn and rave at close of day;",
                                             @"Rage, rage against the dying of the light."];
        
        self.noticeTitles= @[@"Do not go gentle into that good night,",
                                             @"Old age should burn and rave at close of day;",
                                             @"Rage, rage against the dying of the light."];
        
        [self.adMarqueValue reloadData];
        [self.noticeMarqueValue reloadData];
    }
    return self;
}

#pragma mark - UUMarqueeViewDelegate
- (NSUInteger)numberOfVisibleItemsForMarqueeView:(UUMarqueeView*)marqueeView {
    return 1;
}

- (NSUInteger)numberOfDataForMarqueeView:(UUMarqueeView*)marqueeView {
    
    if (marqueeView == self.adMarqueValue) {
        return self.adTitles.count;
    }else{
        return self.noticeTitles.count;
    }
}
- (void)createItemView:(UIView*)itemView forMarqueeView:(UUMarqueeView*)marqueeView {

    itemView.backgroundColor = [UIColor whiteColor];
    UILabel *content = [[UILabel alloc] initWithFrame:itemView.bounds];
    content.font = [UIFont systemFontOfSize:AD(13)];
    content.tag = 1001;
    [itemView addSubview:content];
}
- (void)updateItemView:(UIView*)itemView atIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    UILabel *content = [itemView viewWithTag:1001];
    if (marqueeView == self.adMarqueValue) {
        content.text = self.adTitles[index];
    }else{
        content.text = self.noticeTitles[index];
    }
    
}
    
- (CGFloat)itemViewHeightAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    // for upwardDynamicHeightMarqueeView
    UILabel *content = [[UILabel alloc] init];
    content.numberOfLines = 0;
    content.font = [UIFont systemFontOfSize:AD(13)];
    
    if (marqueeView == self.adMarqueValue) {
        content.text = [self.adTitles[index] objectForKey:@"content"];
    }else{
        content.text = [self.noticeTitles[index] objectForKey:@"content"];
    }
    
    
    CGSize contentFitSize = [content sizeThatFits:CGSizeMake(CGRectGetWidth(marqueeView.frame) - 5.0f - 16.0f - 5.0f, MAXFLOAT)];
    return contentFitSize.height + 20.0f;
}

- (CGFloat)itemViewWidthAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    // for leftwardMarqueeView
    UILabel *content = [[UILabel alloc] init];
    content.font = [UIFont systemFontOfSize:AD(13)];
    if (marqueeView == self.adMarqueValue) {
        content.text = self.adTitles[index];
    }else{
        content.text = self.noticeTitles[index];
    }
    return (5.0f + 16.0f + 5.0f) + content.intrinsicContentSize.width;  // icon width + label width (it's perfect to cache them all)
}

- (void)didTouchItemViewAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    if (marqueeView == self.adMarqueValue) {
        NSLog(@"Touch at index %lu - \"%@\"", (unsigned long)index, [self.adTitles[index] objectForKey:@"content"]);
    } else {
        NSLog(@"Touch at index %lu - \"%@\"", (unsigned long)index, [self.noticeTitles[index] objectForKey:@"content"]);
    }
}

    
#pragma mark - lazy
-(UILabel *)adTitleLabel{
    if (!_adTitleLabel) {
        _adTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(20), 0, AD(40), self.height/2)];
        _adTitleLabel.text = @"广告";
        _adTitleLabel.textColor = RGB(118.0, 117.0, 250.0);
        _adTitleLabel.font = [UIFont systemFontOfSize:AD(12)];
    }
    return _adTitleLabel;
}

-(UUMarqueeView *)adMarqueValue{
    if (!_adMarqueValue) {
        _adMarqueValue = [[UUMarqueeView alloc] initWithFrame:CGRectMake(AD(60), 0, self.width-AD(60), self.height/2)];
        _adMarqueValue.delegate = self;
        _adMarqueValue.timeIntervalPerScroll = 2.0f;
        _adMarqueValue.timeDurationPerScroll = 1.0f;
    }
    return _adMarqueValue;
}

-(UILabel *)noticeTitleLabel{
    if (!_noticeTitleLabel) {
        _noticeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(20), self.height/2, AD(40), self.height/2)];
        _noticeTitleLabel.text = @"公告";
        _noticeTitleLabel.textColor = RGB(118.0, 117.0, 250.0);
        _noticeTitleLabel.font = [UIFont systemFontOfSize:AD(12)];
    }
    return _noticeTitleLabel;
}


-(UUMarqueeView *)noticeMarqueValue{
    if (!_noticeMarqueValue) {
        _noticeMarqueValue = [[UUMarqueeView alloc] initWithFrame:CGRectMake(AD(60), self.height/2, self.width-AD(60), self.height/2)];
        _noticeMarqueValue.delegate = self;
        _noticeMarqueValue.timeIntervalPerScroll = 2.0f;
        _noticeMarqueValue.timeDurationPerScroll = 1.0f;
    }
    return _noticeMarqueValue;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(AD(20), _noticeTitleLabel.top, self.width-AD(40), AD(0.5))];
        _lineView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5];
    }
    return _lineView;
}

@end
